{% load mptt_tags %}
{% full_tree_for_model pages.Page as pages %}
<script type="text/javascript">
    jQuery('#id_{{ name }}').wymeditor({
        lang: '{{ language }}',
        skin: 'django',
        skinPath: "{{ PAGES_MEDIA_URL }}javascript/wymeditor/skins/django/",
        updateSelector: '.submit-row input[type=submit],',
        updateEvent: 'click',
        postInit: function(wym) {
            wym.resizable({handles: "s", maxHeight: 600});
            //construct the button's html
            html = "<li class='wym_tools_pagelink'>"
                 + "<a name='PageLink' href='#'"
                 + " title='Page link'"
                 + " style='background-image:"
                 + " url({{ PAGES_MEDIA_URL }}javascript/wymeditor/skins/django/icons.png);"
                 + " background-position-y: -623px;'>"
                 + "Page link"
                 + "</a></li>";

            //add the button to the tools box
            jQuery(wym._box)
            .find(wym._options.toolsSelector + wym._options.toolsListSelector)
            .append(html);

            //construct the dialog's html
            html = "<body class='wym_dialog wym_dialog_pagelink'"
                 + " onload='WYMeditor.INIT_DIALOG(" + WYMeditor.INDEX + ")'"
                 + ">"
                 + "<form>"
                 + "<fieldset>"
                 + "<input type='hidden' class='wym_dialog_type' value='"
                 + "Pages"
                 + "' />"
                 + "<legend>Page link</legend>"
                 + "<div class='row'>"
                 + "<label>Page</label>"
                 + "<select class='wym_select_page_link'>"
                 {% for page in pages %}
                 + "<option value='{{ page.get_absolute_url }}'>{{ page.get_ancestors|tree_path:" > " }}{% if page.get_ancestors %} > {% endif %}{{ page }}</option>"
                 {% endfor %}
                 + "</select>"
                 + "</div>"
                 + "<div class='row'>"
                 + "<label>Title</label>"
                 + "<input type='text' class='wym_title' value='' size='40' />"
                 + "</div>"
                 + "<div class='row row-indent'>"
                 + "<input class='wym_submit wym_submit_pagelink' type='button'"
                 + " value='{Submit}' />"
                 + "<input class='wym_cancel' type='button'"
                 + "value='{Cancel}' />"
                 + "</div>"
                 + "</fieldset>"
                 + "</form>"
                 + "</body>";
            //handle click event on wrap button
            jQuery(wym._box)
            .find('li.wym_tools_pagelink a').click(function() {
                wym.dialog('Page link', html);
                return(false);
            });
        },
        //handle click event on dialog's submit button
        postInitDialog: function( wym, wdw ) {
            var body = wdw.document.body;
            jQuery(body)
                .find('input.wym_submit_pagelink')
                .click(function() {
                    var href  = jQuery(body).find('.wym_select_page_link').val();
                    var title = jQuery(body).find('.wym_title').val();
                    wym.insert('<a href="' + href + '" title="' + title + '">' + title + '</a>');
                    wdw.close();
                });
        },
    });
    </script>
